using UnityEngine;

//设定情景
[CreateAssetMenu]
public class GameScenario : ScriptableObject
{
    [SerializeField]
    EnemyWave[] waves = {};

    //周期数
    [SerializeField, Range(0,10)]
    int cycles = 1;

    //控制每个周期的加速
    [SerializeField, Range(0f,1f)]
    float cycleSpeedUp = 0.5f;


    public State Begin() => new State(this);

    [System.Serializable]
    public struct State
    {
        GameScenario scenario;
        
        int cycle, index;

        float timeScale;

        EnemyWave.State wave;

        public State(GameScenario scenario)
        {
            this.scenario = scenario;
            index = 0;
            cycle = 0;
            timeScale = 1f;
            Debug.Assert(scenario.waves.Length > 0, "Enemty scenario!");
            wave = scenario.waves[0].Begin();
        }

        public bool Progress()
        {
            float deltaTime = wave.Progress(timeScale * Time.deltaTime);
            while(deltaTime >= 0f)
            {
                if(++index >= scenario.waves.Length)
                {
                    if(++cycle >= scenario.cycles && scenario.cycles > 0)
                    {//经过足够周期才结束游戏
                        return false;
                    }
                    index = 0;
                    //每个周期之后，增加速度
                    timeScale += scenario.cycleSpeedUp;
                }
                wave = scenario.waves[index].Begin();
                deltaTime = wave.Progress(deltaTime);
            }
            return true;
        }
    }
}